#include "stdafx.h"
#include "Enemy.h"

#include "AfterBurnerClone.h"
#include "Missile.h"
#include "../Framework/Toolkit.h"

namespace MyGame {

Enemy::Enemy(Game* game) : Jet(game) {
    this->SetDirection(D3DXVECTOR3(0, 0, -1));
    this->SetMaxSpeed(15);
    this->SetMinSpeed(0);
    this->SetSpeed(15);
    this->SetHealth(1);
    this->fireTimer = (FLOAT)Toolkit::RandomFloat(0.0f, 2.0f);
    this->incomingMissile = NULL;
    this->missileSpeed = 20;
    this->missileTurnSpeed = 3.0f;
    this->targeted = false;
    this->YawPitchRollTo(D3DXVECTOR3(0, D3DX_PI, 0));
    this->SetRotation(D3DXVECTOR3(0, D3DX_PI, 0));
    this->SetMeshFile("Mig 15.x");
    this->SetScale(D3DXVECTOR3(0.2f, 0.2f, 0.2f));
}

Enemy::~Enemy(void) {
}

FLOAT Enemy::GetFireTimer() const {
    return this->fireTimer;
}

void Enemy::SetFireTimer(FLOAT val) {
    this->fireTimer = val;
}

Missile* Enemy::GetIncomigMissile() const {
    return this->incomingMissile;
}

void Enemy::SetIncomingMissile(Missile* val) {
    this->incomingMissile = val;
}

Player* Enemy::GetPlayer() const {
    return this->player;
}

void Enemy::SetPlayer(Player* val) {
    this->player = val;
}

bool Enemy::GetTargeted() const {
    return this->targeted;
}

void Enemy::SetTargeted(bool val) {
    this->targeted = val;
}

void Enemy::ShootMissile() {
    Missile* missile = new Missile(this->GetGame());
    missile->SetDirection(this->GetDirection());
    missile->SetPosition(this->GetPosition());
    missile->SetSpeed(this->missileSpeed);
    missile->SetTurnSpeed(this->missileTurnSpeed);
    missile->SetTarget(this->player);
    missile->Initialize();
    missile->LoadContent();
    ((AfterBurnerClone*)this->GetGame())->GetEnemyController()->GetMissiles()->push_back(missile);
}

void Enemy::Update(FLOAT timeDelta) {
    if (this->GetHealth() <= 0) {
        this->SetVisible(false);
    }

    __super::Update(timeDelta);

    if (this->fireTimer > 0 && this->GetState() == FLYINGSTATE_ALIVE) {
        this->fireTimer -= timeDelta;
        if (this->fireTimer <= 0) {
            this->ShootMissile();
        }
    }
}

}
